<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
          <a><cite>首页</cite></a>
          <a><cite>会员管理</cite></a>
          <a><cite>系统日志</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <form class="layui-form x-center" action="" style="width:80%">
            <div class="layui-form-pane" style="margin-top: 15px;">
              <div class="layui-form-item">
                <label class="layui-form-label">日期范围</label>
                <div class="layui-input-inline">
                  <input class="layui-input" placeholder="开始日" id="LAY_demorange_s" name="date-start">
                </div>
                <div class="layui-input-inline">
                  <input class="layui-input" placeholder="截止日" id="LAY_demorange_e" name="date-stop">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline" style="width:80px">
                    <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                </div>
              </div>
            </div> 
        </form>
        <xblock><button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button><span class="x-right" style="line-height:40px">共有数据：{$count} 条</span></xblock>
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" name="select-all" id="select-all" value="">
                    </th>
                    <th>
                        ID
                    </th>
                    <th>
                        类型
                    </th>
                    <th>
                        内容
                    </th>
                    <th>
                        用户名
                    </th>
                    <th>
                        客户端IP
                    </th>
                    <th>
                        时间
                    </th>
                    <th>
                        操作
                    </th>
                </tr>
            </thead>
            <tbody id="x-link">
            <?php foreach ($log_list as $llv): ?>
                <tr>
                    <td>
                        <input type="checkbox" value="{$llv['id']}" name="selected" class="selected">
                    </td>
                    <td>
                        {$llv['id']}
                    </td>
                    <td>
                    <?php if ($llv['type'] == 1) { ?>
                        访问
                    <?php } elseif ($llv['type'] == 2) { ?>
                        增加
                    <?php } elseif ($llv['type'] == 3) { ?>
                        修改
                    <?php } elseif ($llv['type'] == 4) { ?>
                        删除
                    <?php } elseif ($llv['type'] == 5) { ?>
                        登录
                    <?php } ?>
                    </td>
                    <td>
                        <?php if ($llv['more'] != null): ?>
                            <u style="cursor:pointer" onclick="member_show('更多内容','{:U('admin/system/log_more',array('id'=>$llv['id']))}','750','400')">
                                    {$llv['content']}
                            </u>
                        <?php else: ?>
                            {$llv['content']}
                        <?php endif ?>
                    </td>
                    <td>
                        {$llv['aname']}
                    </td>
                    <td >
                        {$llv['ip']}
                    </td>
                    <td >
                        {:timeFormat($llv['time'])}
                    </td>
                    <td class="td-manage">
                        <a title="删除" href="javascript:;" onclick="log_del(this,'{$llv['id']}')" 
                        style="text-decoration:none">
                            <i class="layui-icon">&#xe640;</i>
                        </a>
                    </td>
                </tr>
            <?php endforeach ?>
            </tbody>
        </table>
        <div id="page" style="text-align: center;"></div>
    </div>
    <script>
        layui.use(['element','layer','laypage','laydate','form'], function(){
          var lement = layui.element();//面包导航
          var laydate = layui.laydate;//日期插件
          var layer = layui.layer;//弹出层
          var form = layui.form();//表单插件
          var laypage = layui.laypage;
          var actname = '{$actname}';

          // 日期范围选择
          var start = {
            min: laydate.now()
            ,format: 'YYYY-MM-DD hh:mm:ss' //日期格式
            ,istime: true //是否开启时间选择
            ,min: '1900-01-01 00:00:00' //最小日期
            ,max: '2099-12-31 23:59:59'
            ,istoday: false
            ,choose: function(datas){
              end.min = datas; //开始日选好后，重置结束日的最小日期
              end.start = datas //将结束日的初始值设定为开始日
            }
          };
          
          var end = {
            min: laydate.now()
            ,format: 'YYYY-MM-DD hh:mm:ss' //日期格式
            ,istime: true //是否开启时间选择
            ,min: '1900-01-01 00:00:00' //最小日期
            ,max: '2099-12-31 23:59:59'
            ,istoday: false
            ,choose: function(datas){
              start.max = datas; //结束日选好后，重置开始日的最大日期
            }
          };

          $('#LAY_demorange_s').click(function () {
            start.elem = this;
            laydate(start);
          });

          $('#LAY_demorange_e').click(function () {
            end.elem = this;
            laydate(end);
          });

          // 提交搜索
          form.on('submit(search)', function(data){
             // 转换开始日期
             if (data.field['date-start']) {
                 data.field['date_start'] = data.field['date-start'];
             }
             // 转换结束日期
             if (data.field['date-start']) {
                 data.field['date_stop'] = data.field['date-stop'];
             }
             console.log(data);
             $.post('{:U("admin/system/sys_search")}', {data: data.field}, function(data) {
                // 显示搜索结果
                layer.open({
                    type: 1,
                    area: ['800px', '500px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade:0.4,
                    title: '搜索结果',
                    content: data
                });
             });
             return false;
          });
          // 分页实现
          laypage({
                cont: 'page'
                ,pages: {$allpages} //总页数
                ,groups: 5 //连续显示分页数
                ,crr: '{$pid}'
                ,jump: function(obj, first) {
                    if (!first) {
                        //得到了当前页，用于向服务端请求对应数据
                        var curr = obj.curr;
                        $.ajax({
                            url: '{:U("admin/system/'+actname+'")}',
                            type: 'POST',
                            dataType: 'json',
                            data: {'pid': curr},
                        })
                        .done(function(response) {
                            // console.log(response);
                            var type = '';
                            var more = '';
                            var list = '';
                            for (var data in response.page_list) {
                                if (response.page_list[data].type == 1) {
                                    type = '访问';
                                } else if (response.page_list[data].type == 2) {
                                    type = '增加';
                                } else if (response.page_list[data].type == 3) {
                                    type = '修改';
                                } else if (response.page_list[data].type == 4) {
                                    type = '删除';
                                } else if (response.page_list[data].type == 5) {
                                    type = '登录';
                                }

                                if (response.page_list[data].more != null) {
                                    more = '<u style=\"cursor:pointer\"onclick=\"member_show(\'更多内容\',\''+response.url[data]+'\',\'650\',\'300\')\">'+response.page_list[data].content+'<\/u>';
                                    // console.log(more);
                                } else {
                                    more = response.page_list[data].content;
                                }

                                list += '<tr><td><input type="checkbox"value="'+response.page_list[data].id+'"name="selected"></td><td>'+response.page_list[data].id+'</td><td>'+type+'</td><td>'+more+'</td><td>'+response.page_list[data].aname+'</td><td>'+response.page_list[data].ip+'</td><td>'+response.page_list[data].time+'</td><td class="td-manage"><a title="删除"href="javascript:;"onclick="log_del(this,'+response.page_list[data].id+')"style="text-decoration:none"><i class="layui-icon">&#xe640;</i></a></td></tr>';
                            }
                            $('#x-link').empty().prepend(list);
                            $('input[name=select-all]').prop('checked',false);
                        })
                        .fail(function() {
                            console.log("error");
                        });
                    }
                }
            });
        });

          
        //全选
        $('#select-all').on('click', function () {
            if ($(this).is(':checked')) {
                $('input[name=selected]').prop("checked",true);
                $(this).prop("checked",'checked',true);
            } else {
                $(this).prop('checked',false);
                $('input[name=selected]').prop('checked',false);
            }
        });
        //单选(全选后取消一项全选按钮个勾选状态消失)
        $("#x-link").on("change",".selected", function() {
            var count = $(".selected:checked").length;
            if (count == $('tr').length - 1) {
                $('#select-all').prop('checked',true);
            } else {
                $('#select-all').prop('checked',false);
            }
        });
        
        //批量删除提交
         function delAll () {
            // console.log();
            layer.confirm('确认要删除吗？',function(index){
                //捉到所有被选中的，发异步进行删除
                var id = '';
                $($("input[name=selected]:checked")).each(function(){
                    id+=this.value + ',';    //遍历被选中CheckBox元素的集合 得到Value值
                });
                id=id.substring(0,id.length-1)
                if (id=='' || id==undefined || id==null) {
                    layer.msg('至少选择一条删除项', {icon: 2});
                    return false;
                }
                $.post('{:U("admin/system/sys_link")}', {del: id}, function(response) {
                    console.log(response);
                    layer.msg('删除成功', {icon: 1});
                    $('input[name=select-all]').prop('checked',false);
                    window.location.reload();
                });
            });
         }
       
        /*删除*/
        function log_del(obj,id){
            layer.confirm('确认要删除吗？',function(index){
                // 发异步删除数据
                $.ajax({
                    url: '{:U("admin/system/sys_log")}',
                    type: 'POST',
                    dataType: 'json',
                    data: {del: id},
                })
                .done(function(response) {
                    if (response == 1) {
                        layer.msg('已删除!',{icon:1,time:1000});
                    }
                })
                .fail(function() {
                    layer.msg('删除失败!',{icon:1,time:1000});
                });
                
                $(obj).parents("tr").remove();
                layer.msg('已删除!',{icon:1,time:1000});
            });
        }

        /*查看更多详细内容*/
        function member_show(title,url,w,h){
            x_admin_show(title,url,w,h);
        }
        </script>
</body>